import './popup.scss';
import { Request } from '../models/request.model';
import { TabManager } from './components/TabManager';
import { RequestEditor } from './components/RequestEditor';
import { RequestsManager } from './components/RequestsManager';
import { SavedRequestsManager } from './components/SavedRequestsManager';
import { MessageService, MessageType } from '../services/message.service';

/**
 * 弹出页面管理类
 * 负责处理弹出页面的UI和交互的主类
 */
class PopupManager {
    private tabManager: TabManager;
    private requestEditor: RequestEditor;
    private requestsManager: RequestsManager;
    private savedRequestsManager: SavedRequestsManager;

    /**
     * 构造函数
     */
    constructor() {
        // 请求选择处理函数
        const handleRequestSelect = (request: Request) => {
            this.requestEditor.loadRequest(request);
            this.tabManager.switchTab('tab-current');
        };
        
        // 初始化请求编辑器
        this.requestEditor = new RequestEditor(
            // 请求发送完成后的回调
            () => {
                // 什么都不做，移除了历史记录功能
            },
            // 请求保存后的回调
            () => {
                // 刷新已保存的请求列表
                this.savedRequestsManager.loadSavedRequests();
            }
        );
        
        // 初始化组件
        this.tabManager = new TabManager();
        this.requestsManager = new RequestsManager(handleRequestSelect);
        this.savedRequestsManager = new SavedRequestsManager(handleRequestSelect);
        
        // 加载当前标签页的请求
        this.loadActiveTabRequest();
    }

    /**
     * 加载当前活动标签的请求
     */
    private async loadActiveTabRequest(): Promise<void> {
        try {
            console.log('正在加载当前标签页请求');
            
            // 发送消息获取当前标签的请求
            const response = await MessageService.sendMessage({
                type: MessageType.GET_ACTIVE_TAB_REQUEST
            });
            
            console.log('收到活动标签页请求响应:', response);

            if (response && response.success && response.request) {
                try {
                    // 更新当前请求
                    console.log('正在创建Request对象:', response.request);
                    const request = new Request(response.request);
                    console.log('创建的Request对象:', request);
                    this.requestEditor.loadRequest(request);
                } catch (e) {
                    // 处理Request创建或加载过程中的错误
                    const errorMessage = e instanceof Error ? e.message : String(e);
                    console.error('创建或加载Request对象时出错:', errorMessage);
                }
            } else if (response) {
                console.warn('活动标签页请求响应无数据:', response.error || '未知错误');
            } else {
                console.warn('活动标签页请求响应为空');
            }
        } catch (e) {
            // 处理整体错误
            const errorMessage = e instanceof Error ? e.message : String(e);
            console.error('加载当前标签页请求时出错:', errorMessage);
        }
    }
}

// 当DOM加载完成后初始化弹出页面
document.addEventListener('DOMContentLoaded', () => {
    console.log('DOM已加载完成，初始化PopupManager');
    new PopupManager();
}); 